package my.avoskagae.client.presenter;

import java.util.List;

import my.avoskagae.client.eventbus.MainBus;
import my.avoskagae.client.view.ICatalogListView;
import my.avoskagae.client.view.gwt.CatalogListView;
import my.avoskagae.model.entity.Product;
import my.avoskagae.service.abstr.AvoskaServiceAsync;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = CatalogListView.class)
public class CatalogListPresenter extends
		BasePresenter<ICatalogListView, MainBus> implements
		ICatalogListPresenter {


	@Inject
	AvoskaServiceAsync serviceAsync;
	
	public void onShowCatalog() {
		serviceAsync.getProducts(new AsyncCallback<List<Product>>() {

			@Override
			public void onSuccess(List<Product> result) {
				view.refreshData(result);
				eventBus.setMainList(view.getWidgetItself()); // TODO почему в шоу каталог?
				eventBus.changeSelectionProduct(view.getSelectionProduct());
			}

			@Override
			public void onFailure(Throwable caught) {
				view.showAlert("Ошибка: " + caught.getMessage());
			}
		});
	}

	public void onChangeCatalog() {
		serviceAsync.getProducts(new AsyncCallback<List<Product>>() {

			@Override
			public void onSuccess(List<Product> result) {
				view.refreshData(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				view.showAlert("Ошибка: " + caught.getMessage());
			}
		});

	}

	@Override
	public void changeSelectionProduct() {
		eventBus.changeSelectionProduct(view.getSelectionProduct());
	}

	@Override
	public void createProduct(String name) {

		serviceAsync.createProduct(name, new AsyncCallback<Product>() {

			@Override
			public void onSuccess(Product result) {
				view.setSelectionProduct(result);
				//FIXME выделяется не то
				eventBus.changeSelectionProduct(result);
				eventBus.changeCatalog();
			}

			@Override
			public void onFailure(Throwable caught) {
				view.showAlert("Ошибка: " + caught.getMessage());
			}
		});

	}

	@Override
	public void deleteProduct(final Product product) {

		serviceAsync.removeProduct(product, new AsyncCallback<Boolean>() {

			@Override
			public void onSuccess(Boolean result) {
				view.resetSelectionProduct(product);
				eventBus.changeCatalog();
			}

			@Override
			public void onFailure(Throwable caught) {
				view.showAlert("Ошибка: " + caught.getMessage());
			}
		});

	}

	@Override
	public void renameProduct(Product product, String newName) {
		serviceAsync.renameProduct(product, newName,
				new AsyncCallback<Product>() {

					@Override
					public void onSuccess(Product result) {
						//FIXME не правильно обновляются ProductDetail
						eventBus.changeCatalog();
						eventBus.changeSelectionProduct(view.getSelectionProduct());
					}

					@Override
					public void onFailure(Throwable caught) {
						view.showAlert("Ошибка: " + caught.getMessage());
					}
				});

	}

	@Override
	public void switchNeedProduct(Product object) {
		serviceAsync.switchNeedPurchase(object, !object.getNeedPurchase(), new AsyncCallback<Product>() {
			
			@Override
			public void onSuccess(Product result) {
				//FIXME не правильно обновляются ProductDetail
				eventBus.changeCatalog();
				eventBus.changeSelectionProduct(view.getSelectionProduct());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				view.showAlert("Ошибка: " + caught.getMessage());				
			}
		});
	}

	public AvoskaServiceAsync getServiceAsync() {
		return serviceAsync;
	}

	public void setServiceAsync(AvoskaServiceAsync serviceAsync) {
		this.serviceAsync = serviceAsync;
	}

}
